/*
 * Main.java
 *
 * Created on 25-Apr-2009, 07:00:59
 * Copyright scalan
 */
package net.ScAlAn;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import net.scalan.jpa.model.DBArguments;
import net.scalan.jpa.model.DBCabinetCasette;
import net.scalan.jpa.model.DBCasetteModule;
import net.scalan.jpa.model.DBNotification;
import net.scalan.jpa.model.DBSchemObject;
import net.scalan.jpa.model.DBSchema;
import net.scalan.jpa.model.DBStation;
import net.scalan.jpa.model.DBStationCabinet;
import net.scalan.jpa.model.DBTypeOfModuleContact;

public class Main implements Serializable {

    private EntityManagerAccessBean emf;

    private long station_id;

    private DBStation station;

    private List<DBSchema> schemas;

    public Main() {
        System.out.println("Main.constructor()...");
    }

    /**
     * @param emf the emf to set
     */
    public void setEmf(EntityManagerAccessBean emf) {
        this.emf = emf;
    }

    /**
     * @return the station_id
     */
    public long getStation_id() {
        return station_id;
    }

    /**
     * @param station_id the station_id to set
     */
    public void setStation_id(long station_id) {
        this.station_id = station_id;
        if (station_id > 0) {
            EntityManager em = emf.getEntityManager();
            if (em != null) {
                try {
                    station = (DBStation) em.find(DBStation.class, new Long(station_id));
                    if (station != null) {
                        schemas = station.getSchemas();
                        if (getSchemas() != null && getSchemas().size() > 0) {
                            for (DBSchema schema : getSchemas()) {
                                if (schema != null) {
                                    if (schema.getSchemObject() != null && schema.getSchemObject().size() > 0) {
                                        for (DBSchemObject so : schema.getSchemObject()) {
                                            //get current objectct's value
                                        }
                                    }
                                }
                            }
                        }
                        if (station.getCabinets() != null && station.getCabinets().size() > 0) {
                            for (DBStationCabinet cabinet : station.getCabinets()) {
                                if (cabinet.getCasettes() != null && cabinet.getCasettes().size() > 0) {
                                    for (DBCabinetCasette casette : cabinet.getCasettes()) {
                                        if (casette.getModules() != null && casette.getModules().size() > 0) {
                                            for (DBCasetteModule module : casette.getModules()) {
                                                if (module.getType() != null && module.getType().getContacts() != null && module.getType().getContacts().size() > 0) {
                                                    for (DBTypeOfModuleContact contact : module.getType().getContacts()) {
                                                        //get current contact's value
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                } finally {
                    em.close();
                }
            }
        }
    }

    /**
     * @return the schemas
     */
    public List<DBSchema> getSchemas() {
        return schemas;
    }

    /**
     * @return the station
     */
    public DBStation getStation() {
        return station;
    }

    //==========================================================================
    private boolean archMode = false; //

    /**
     * @return the archMode
     */
    public boolean isArchMode() {
        return archMode;
    }

    public void changeArchMode() {
        archMode = !archMode;
    }

    //==========================================================================
    /**
     * @return the notifications
     */
    public List<DBNotification> getNotifications() {
        List<DBNotification> notifications = new ArrayList<DBNotification>();
        if (station_id > 0) {
            EntityManager em = emf.getEntityManager();
            if (em != null) {
                try {
                    Query q = em.createQuery(
                            "SELECT d "
                            + "FROM DBNotification AS d "
                            + "WHERE d.station = :station "
                            + "ORDER BY d.dt DESC");
                    q.setParameter("station", station);
                    q.setMaxResults(10);
                    notifications = q.getResultList();
                } finally {
                    em.close();
                }
            }
        }
        return notifications;
    }
}

